package com.punai.datapush.controller;

import java.io.BufferedInputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.util.StrUtil;
import com.punai.common.utils.DateUtil;
import com.punai.common.utils.file.FileUtils;
import com.punai.customer.domain.CusFeature;
import com.punai.customer.domain.bo.FeatureRes;
import com.punai.customer.domain.contants.CusConstants;
import com.punai.datapush.constants.DataPushConstants;
import com.punai.datapush.domain.EcoFeeitemPeriod;
import com.punai.datapush.domain.bo.EcoFeeitemGuidepriceBo;
import com.punai.datapush.domain.vo.EcoGuidePriceAddVo;
import com.punai.datapush.domain.vo.GuidepriceExcelImpVo;
import com.punai.datapush.domain.vo.GuidepriceVo;
import com.punai.datapush.service.IEcoFeeitemPeriodService;
import com.punai.standard.domain.bo.FeatureBo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.punai.common.annotation.Log;
import com.punai.common.core.controller.BaseController;
import com.punai.common.core.domain.AjaxResult;
import com.punai.common.enums.BusinessType;
import com.punai.datapush.domain.EcoFeeitemGuideprice;
import com.punai.datapush.service.IEcoFeeitemGuidepriceService;
import com.punai.common.utils.poi.ExcelUtil;
import com.punai.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 指导价格库Controller
 * 
 * @author zxw_
 * @date 2023-04-25
 */
@RestController
@RequestMapping("/datapush/guideprice")
public class EcoFeeitemGuidepriceController extends BaseController
{
    @Autowired
    private IEcoFeeitemGuidepriceService ecoFeeitemGuidepriceService;
    @Autowired
    private IEcoFeeitemPeriodService feeitemPeriodService;

    Logger log = LoggerFactory.getLogger(EcoFeeitemGuidepriceController.class);



    /**
     * 查询指导价格库列表
     */
    @PreAuthorize("@ss.hasPermi('datapush:guideprice:treelist')")
    @GetMapping("/datalist")
    public TableDataInfo datalist(GuidepriceVo vo)
    {
        TableDataInfo tableDataInfo = ecoFeeitemGuidepriceService.datalist(vo);
        return tableDataInfo;
    }
    @PreAuthorize("@ss.hasPermi('datapush:guideprice:query')")
    @GetMapping("/getStaFee/{id}/{versionId}")
    public AjaxResult getStaFeeFeature(@PathVariable String id,@PathVariable String versionId) {
        List<FeatureRes> list = ecoFeeitemGuidepriceService.getStaFeeFeature(id,versionId,getCompanyId());
        return AjaxResult.success(list);
    }
    @PreAuthorize("@ss.hasPermi('datapush:guideprice:addedit')")
    @PostMapping("/add")
    public AjaxResult add(@RequestBody EcoGuidePriceAddVo vo) {
        return AjaxResult.success(ecoFeeitemGuidepriceService.add(vo));
    }

    @PreAuthorize("@ss.hasPermi('datapush:guideprice:query')")
    @GetMapping("/getInfo/{id}")
    public AjaxResult getInfo(@PathVariable String id) {
        return AjaxResult.success(ecoFeeitemGuidepriceService.getInfo(id));
    }

    @PreAuthorize("@ss.hasPermi('datapush:guideprice:remove')")
    @DeleteMapping("/deleteInfo/{id}")
    public AjaxResult deleteInfo(@PathVariable String id) {
        return AjaxResult.success(ecoFeeitemGuidepriceService.deleteInfo(id));
    }

    // 导出excel
    @PreAuthorize("@ss.hasPermi('datapush:guideprice:export')")
    @Log(title = "指导价格库", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response,GuidepriceVo vo)
    {
        List<EcoFeeitemGuidepriceBo> list = ecoFeeitemGuidepriceService.selectEcoFeeitemGuidepriceList(vo);
        ExcelUtil<EcoFeeitemGuidepriceBo> util = new ExcelUtil<>(EcoFeeitemGuidepriceBo.class);

        String qc = vo.getQc();
        EcoFeeitemPeriod period = feeitemPeriodService.getById(qc);
        String name = period.getName();
        Date start = period.getStart();
        Date end = period.getEnd();
        String startStr = DateUtil.dateToyMd(start);
        String endStr = DateUtil.dateToyMd(end);
        util.exportExcel(response,list, name + "(" + startStr + "~" + endStr + ")");
    }

    @Log(title = "材料费用项-下载模板", businessType = BusinessType.UPDATE)
    @PostMapping("/downloadGuidePriceExcelTemplate")
    public void downloadMaterialExcelTemplate(HttpServletResponse response)
    {
        try
        {
            String guidepriceImpExcelName = DataPushConstants.GUIDEPRICE_IMP_EXCEL_NAME;
            Resource resource = new ClassPathResource("template/" + guidepriceImpExcelName);
            //放弃使用直接获取文件的方式, 改成使用流的方式
            BufferedInputStream bis = new BufferedInputStream(resource.getInputStream());
            response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
            FileUtils.setAttachmentResponseHeader(response, "指导价导入模板.xlsx");
            FileUtils.downloadFile(bis, response.getOutputStream());
        }
        catch (Exception e)
        {
            log.error("下载文件失败", e);
        }
    }

    // 导入excel
    @PreAuthorize("@ss.hasPermi('datapush:guideprice:import')")
    @Log(title = "指导价格库", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(@RequestBody GuidepriceExcelImpVo guidepriceExcelImpVo)
    {
        String resultStr = ecoFeeitemGuidepriceService.importData(guidepriceExcelImpVo);
        if (StrUtil.isBlank(resultStr)){
            return AjaxResult.success();
        }
        return AjaxResult.success(resultStr);
    }

}
